package vmcs.store;

// Copyright 1997 ISS.
// The contents contained in this document may not be reproduced in any
// form or by any means, without the written permission of ISS, other
// than for the purpose for which it has been supplied.
//
// Author: Olivo Miotto
//
//

import java.util.*;

import vmcs.common.VMCSException;
import vmcs.customer.coinstrategy.ICoinValidationStrategy;
import vmcs.customer.coinstrategy.WeightCoinValidationStrategy;

public class CashStore extends Store {
private ICoinValidationStrategy strategy;

public final static int INVALID_COIN_WEIGHT = 9999;
	private static CashStore cashStore;
    private CashStore () {
    	
    }
    
    public static CashStore getInstance(){
    	if(cashStore==null){
    		cashStore = new CashStore();
    	}
    	return cashStore;
    }
    /**
     * This operation is used to find the index to a Cash Store Item for a specific 
     * Coin object.
     * @param c  The current Coin object.
     * @return   returns an integer of the index.  If no Coin is found, returns -1.
     * @throws VMCSException 
     */
    public int findCashStoreIndex (Coin c) throws VMCSException {
            int size = getStoreSize();
            for (int i = 0; i < size; i++) {
                    StoreItem item = (CashStoreItem) getStoreItem(i);
                    Coin current = (Coin) item.getContent();
                    if (current.getWeight() == c.getWeight())
                            return i;
            }
            return -1;
    }

    /**
     * This operations returns the Coin object if the Coin is valid.
     * @param coin: Coin
     * @return Coin
     */
    public Coin findCoin(Coin coin) throws Exception{
    	/*  double en;
        	Coin coin; 
            StoreObject so;
            int i;

            for (i = 0; i < size; i++) {
                    if (items[i] == null)
                            return null;
                    so = items[i].getContent();
                    if (so == null)
                            return null;
                    coin = (Coin)so;
                    en = coin.getWeight();
                    if (en == weight)
                            return coin;
            }
            */
    	if(strategy!=null)
    	{
    		coin  = strategy.validateCoin(this.getItems(), coin);
    	}
    	else{
    		throw new Exception("Validation Strategy Has not been defined");
    	}
        return coin;
    }
    public ICoinValidationStrategy getStrategy() {
    	return strategy;
    }

    public void setStrategy(ICoinValidationStrategy strategy) {
    	this.strategy = strategy;
    }
}

